﻿/* Serialize Plugin Context
 * Copyright Andrew Swerlick - Engage, Inc. 2010
 * is free software: you can redistribute it and/or modify it under the terms of the 
 * GNU General Public License as published by the Free Software Foundation, 
 * either version 3 of the License, or (at your option) any later version.
 * Serialize Plugin Context is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with Serialize Plugin Context. 
 * If not, see http://www.gnu.org/licenses/.
 ***********************************************/

using System;
using System.IO;
using System.Runtime.Serialization;

using Microsoft.Xrm.Sdk;
using SerializePluginContext;

namespace Engage.Util.PluginTester
{
    /// <summary>
    /// Class with a series of static method to help with plugin debugging. 
    /// </summary>
    public class PluginTester
    {
        /// <summary>
        /// Takes the path to the file containing the serialized plugin context generated by the SerializePluginContext plug-in.
        /// </summary>
        /// <param name="serializedContextPath"></param>
        /// <returns>The deserialized Plugin, as a MyPluginContext object</returns>
        public static MyPluginContext DeSerializePluginContext(string serializedContextPath)
        {
            Type[] knownTypes = new Type[] {typeof(Entity)};
            DataContractSerializer formatter = new DataContractSerializer(typeof(MyPluginContext), knownTypes);
            MyPluginContext myContext = new MyPluginContext();
            FileStream file = File.Open(serializedContextPath, FileMode.Open);
            lock (file)
            {
                myContext = (MyPluginContext)formatter.ReadObject(file);
                file.Flush();
                file.Close();
            }
            return myContext;
        }
    }
}
